package com.jk.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.github.pagehelper.PageHelper;
import com.jk.bean.Content;
import com.jk.bean.Finance;
import com.jk.bean.Page;
import com.jk.bean.SendPage;
import com.jk.mapper.BooMapper;
import com.jk.service.BooService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class BooServiceImpl implements BooService {
    @Resource
    private BooMapper booMapper;


    @Autowired
    private MongoTemplate mongoTemplate;


    @Override
    public SendPage queryBoo(Page page,String name) {
         if(!name.equals("all")){
             List<Finance> list1=booMapper.queryBoo(name);
             PageHelper.startPage(page.getPage(),page.getRows());
             List<Finance> list=booMapper.queryBoo(name);
             SendPage sendPage = new SendPage(list1.size(),list);
             return sendPage;
         }else{
              List<Finance> list3=booMapper.queryBooAll();

             PageHelper.startPage(page.getPage(),page.getRows());
             List<Finance> list2=booMapper.queryBooAll();

             SendPage sendPage2 = new SendPage(list3.size(),list2);
             return sendPage2;
         }

    }

    @Override
    public List<Finance> getAllTables() {

        return booMapper.getAllTables();
    }

    @Override
    public List<Content> queryXqing(String pid, String tablename) {
        Query query = new Query();


        if(!StringUtils.isEmpty(pid)) {
            query.addCriteria(Criteria.where("nameid").is(pid));
        }
        if(!StringUtils.isEmpty(tablename)) {
            query.addCriteria(Criteria.where("tablename").is(tablename));
        }
        List<Content> cn= mongoTemplate.find(query, Content.class);
        return  cn;
    }

    @Override
    public Finance getBoo(String id,String name) {
        Finance boo = booMapper.getBoo(id, name);
        return boo;
    }
}
